import random

import time

from poetry.author import Author
from poetry.sqlTool import MySqlTool

import requests
from bs4 import BeautifulSoup

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5)'
headers = {'User-Agent': user_agent}
session = requests.session()
sqlTool = MySqlTool()

def getAsleep():
    sleepTime = random.uniform(0, 2)
    print("休眠：", sleepTime)
    time.sleep(sleepTime)


def getUrlData( url ,mTime):
    # print("--------------------------------------")
    # print(url)
    page = session.get(url, headers=headers)
    soup = BeautifulSoup(page.text, 'lxml')
    # contents = soup.findAll('div', class='left')
    datas = soup.findAll(name='div', attrs={"class": "left"})
    for data in datas:
        items  = data.findAll( name= 'div',attrs={"class": "sonspic"})
        for item in items:
            urlDiv  =  item.find( name= 'div',attrs={"class": "divimg"})
            if not urlDiv is None:
                imgUrl= urlDiv.find(name= 'img')
                name= imgUrl['alt']
                pic= imgUrl['src']
                descP =  item.find( name= 'p',attrs={"style": " margin:0px;"})
                desc= descP.text
                pos = desc.index("►")  # 去掉无用的内容
                desc = desc[0:pos]

                author = Author(name, "", desc, pic, mTime, "")
                author.printInfo()
                sqlTool.addAuthor(author)
                print("--------------------------------------")


dict = {
    # "先秦": 4,
    # "两汉": 10,
    "魏晋": 13,
    "南北朝": 20,
    "隋代": 3,
    "唐代": 232,
    "五代": 4,
    "宋代": 583,
    "金朝": 10,
    "元代": 64,
    "明代": 279,
    "清代": 598
}


for mkey in dict:
    index = 0
    sum = dict[mkey]
    while index <= sum:
        url = "https://so.gushiwen.org/authors/Default.aspx?p=%d&c=%s" % (index, mkey)
        getUrlData(url, mkey)
        index = index + 1
        getAsleep()

sqlTool.closeConnection()


